package com.jueban.controller;

import com.jueban.Dto.AnnouncementQueryDto;
import com.jueban.Vo.AnnouncementVo;
import com.jueban.Vo.AnnouncementsVo;
import com.jueban.Vo.ResultVo;
import com.jueban.model.Announcement;
import com.jueban.service.AnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

@RestController
@RequestMapping("/api/announcement")
public class AnnouncementController
{

    private final AnnouncementService announcementService;

    @Autowired
    public AnnouncementController(AnnouncementService announcementService) {
        this.announcementService = announcementService;
    }

    //添加一个公告
    @PostMapping("")
    public ResultVo addAnnouncement(@RequestBody @Valid Announcement announcement)
    {

        return announcementService.addAnnouncement(announcement);

    }

    //删除一个公告
    @DeleteMapping("/{id}")
    public ResultVo deleteAnnouncement(@PathVariable("id") Integer id)
    {
        return announcementService.deleteAnnouncementById(id);
    }

    //修改一个公告
    @PutMapping("")
    public ResultVo updateAnnouncement(@RequestBody Announcement announcement)
    {
        return announcementService.updateAnnouncement(announcement);
    }

    //根据ID查询一条公告
    @GetMapping("/{id}")
    public AnnouncementVo getAnnouncementById(@PathVariable("id") Integer id)
    {
        return announcementService.getAnnouncementById(id);
    }

    //根据条件分页查询公告
    @PostMapping("/announcements")
    public AnnouncementsVo findAnnouncement(@RequestBody AnnouncementQueryDto announcementQueryDto)
    {
        return announcementService.getAnnouncements(announcementQueryDto);
    }

}
